package org.zkoss.zktc.unittest;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.zkoss.zktc.core.junit.TestCaseBase;
import org.zkoss.zktc.core.widget.Widget;
import org.zkoss.zktc.core.widget.WidgetDriver;

public class TextboxTestCase extends TestCaseBase{

	protected String getTestCaseUrl(){
		return getTestCaseUrl("/unit/textbox.zul");
	}
	
	@Test
	public void testKeypress(){
		navigate(getTestCaseUrl());
		
		Widget tb = findWidget("$textbox");
		Assert.assertNotNull(tb);
		Assert.assertEquals("zul.inp.Textbox", tb.getAttribute("className"));
		
		
		tb.keys("Cheese");
		tb.tab();
		Assert.assertEquals("Cheese", tb.getValue());
		tb.keys("Cheese\t");
		Assert.assertEquals("CheeseCheese", tb.getValue());
		waitForTrip(1, 1000);
		
		Widget message = findWidget("$message");
		Assert.assertNotNull(message);
		Assert.assertEquals("zul.wgt.Label", message.getAttribute("className"));
		Assert.assertEquals("CheeseCheese", message.getValue());
		
		
		tb.clear(0,0);
		findWidget("$btn").click();
		Assert.assertEquals("", tb.getValue());
		Assert.assertEquals("", findWidget("$message").getValue());
		
	}
	
	@Test
	public void testTab(){
		WidgetDriver driver = getDriver();
		navigate(getTestCaseUrl());
		
		Widget tb = findWidget("$textbox");
		Assert.assertNotNull(tb);
		Assert.assertEquals("zul.inp.Textbox", tb.getAttribute("className"));
		
		
		tb.keys("Tab me").tab();
//		findWidget("$btn").click();
		
		Assert.assertEquals("Tab me", tb.getValue());
		Assert.assertEquals("Tab me", findWidget("$message").getValue());
	}
	
	@Test
	public void testReplace(){
		navigate(getTestCaseUrl());
		
		Widget tb = findWidget("$textbox");
		Assert.assertNotNull(tb);
		Assert.assertEquals("zul.inp.Textbox", tb.getAttribute("className"));
		
		
		tb.replace("Tab me1").tab();
		Assert.assertEquals("Tab me1", tb.getValue());
		Assert.assertEquals("Tab me1", findWidget("$message").getValue());
		
		
		tb.replace("Tab me2").tab();
		Assert.assertEquals("Tab me2", tb.getValue());
		Assert.assertEquals("Tab me2", findWidget("$message").getValue());
		
	}
	
}
